/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Validator;

import Controller.UserController;
import Model.UserBean;
import dao.UserDAO;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author wrjohns
 */
@FacesValidator("Validator.PasswordValidator")
public class PasswordValidator implements Validator {

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        try {
            UserDAO.init();
            //Fix This
            UserBean bean = (UserBean) FacesContext.getCurrentInstance().
                    getExternalContext().getSessionMap().get("userBean");


            String check = UserDAO.getPassword(bean);

            if (check.compareTo(value.toString()) != 0) {
                FacesMessage msg = new FacesMessage("Password Is Incorrect");
                throw new ValidatorException(msg);
            } 
        } catch (SQLException e) {
            Logger.getLogger(PasswordValidator.class.getName()).log(Level.SEVERE, null, e);
            FacesMessage msg = new FacesMessage("Password Is Incorrect");
            throw new ValidatorException(msg);
        } finally {
            try {
                UserDAO.terminate();
            } catch (SQLException e) {
                Logger.getLogger(PasswordValidator.class.getName()).log(Level.SEVERE, null, e);
            }
        }
    }
}
